package rexsee.natives;

import java.io.File;

import rexsee.core.browser.Browser;
import rexsee.core.style.RexseeDrawable;
import rexsee.core.style.StyleSheet;
import rexsee.core.utilities.Utilities;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class ListViewAdapter extends BaseAdapter {

	public final Browser mBrowser;
	private final Drawable mIconBg;
	private final StyleSheet mStyle;
	private final String[] mDrawablePathes;
	private final View[] mViews;

	public ListViewAdapter(Browser browser, ViewTag parent, String[] options) {
		mBrowser = browser;
		mDrawablePathes = options;
		mViews = new View[options.length];
		mStyle = parent.style;
		mIconBg = RexseeDrawable.getDrawable(browser, parent.style.icon_background_color, null);
	}
	public ListViewAdapter(Browser browser, ViewTag parent, View[] options) {
		mBrowser = browser;
		mDrawablePathes = null;
		mIconBg = null;
		mStyle = null;
		mViews = new View[options.length];
		for (int i = 0; i < options.length; i++) {
			mViews[i] = options[i];
			FrameLayout.LayoutParams layoutParams;
			ViewTag tag = (ViewTag) mViews[i].getTag();
			if (tag != null) {
				layoutParams = new FrameLayout.LayoutParams(tag.style.getWidth(), tag.style.getHeight());
			} else {
				layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);
			}
			mViews[i].setLayoutParams(layoutParams);
		}
	}

	private ImageView createImageView(String path) {
		try {
			ImageView imageView = new ImageView(mBrowser.getContext());
			imageView.setLayoutParams(mStyle.getIconLayoutParams());
			imageView.setPadding(mStyle.getIconPaddingLeft(), mStyle.getIconPaddingTop(), mStyle.getIconPaddingRight(), mStyle.getIconPaddingBottom());
			if (mIconBg != null) imageView.setBackgroundDrawable(mIconBg);
			imageView.setAlpha(mStyle.getIconAlpha());
			imageView.setScaleType(mStyle.getIconScaleType());

			int bounds = Utilities.getInt(mStyle.icon_bounds, -1);
			if (bounds > 0) {
				File file = new File(Uri.parse(path).getSchemeSpecificPart());
				if (file != null && file.isFile() && file.canRead()) {
					Bitmap bitmap;
					BitmapFactory.Options options = new BitmapFactory.Options();
					options.inJustDecodeBounds = true;
					bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
					options.inJustDecodeBounds = false;
					options.inSampleSize = Math.max(1, Math.round(Math.max(options.outHeight / bounds, options.outWidth / bounds)));
					bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
					imageView.setImageBitmap(bitmap);
				}
			} else {
				Drawable drawable = RexseeDrawable.getDrawable(mBrowser, path, null);
				if (drawable != null) {
					imageView.setImageDrawable(drawable);
				} else {
					if (mStyle.icon != null) {
						imageView.setImageDrawable(mStyle.icon);
					} else {
					}
				}
			}
			return imageView;
		} catch (Exception e) {
			mBrowser.exception(this.getClass().getSimpleName(), e);
			return null;
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = new FrameLayout(mBrowser.getContext());
		}
		FrameLayout frame = (FrameLayout) convertView;
		if (mDrawablePathes != null) {
			frame.removeAllViews();
			frame.addView(createImageView(mDrawablePathes[position]));
		} else {
			ViewGroup viewParent = (ViewGroup) mViews[position].getParent();
			if (viewParent != null) {
				viewParent.removeAllViews();
			}
			frame.removeAllViews();
			frame.addView(mViews[position]);
		}
		return frame;
	}
	@Override
	public int getCount() {
		return mViews.length;
	}
	@Override
	public Object getItem(int position) {
		return position;
	}
	@Override
	public long getItemId(int position) {
		return position;
	}

}
